$(function(){
    _gridInit();
    $('li[data-opt="add"]').on('click', addCode);
    $("li[data-opt='opt_search']").find(">i").on('click', function(){
        gridSearch.Content = {
            SearchText: $("#opt_search").find(">input").val()
        };
        search();
    });
});

function _gridInit(){
    var _postData = {
        Method: 'Q',
        Content:{
            SearchText: $("#opt_search").find(">input").val()
        }
    };
    for(i in postData){
        _postData[i] = postData[i];
    }
    gridWidth = $('.u_toolbar');
    var _gridData = Object.create(gridData);
    _gridData.sortname = "Code";
    _gridData.url = mdmPath + '/MDM0014/MDM001402';
    _gridData.colModel = [
        {label:'代码', name:'Code'},
        {label:'名称', name:'Name'},
        {label:'营业执照单位', name:'CompanyName'},
        {label:'营业执照开户行', name:'OpeningBank'},
        {label:'营业执照账号', name:'BankAccount'},
        {label:'营业执照税号', name:'DutyNo'},
        {label:'营业执照地址', name:'Laddress'},
        {label:'营业执照电话', name:'LPhone'},
        {label:'营业执照信用代码', name:'CreditCode'},
        // {label:'状态', name:'Status'}
    ];
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            postData : _postData,
        });
    };
    _gridData.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
    }
    _gridData.ondblClickRow = function(rowid,iRow,iCol,e){
        var rowData = _gridData.grid.jqGrid("getRowData", rowid);
        window.open('MDM001402.html?Code=' + rowData['Code'], '_blank');
    }
    gridInit(_gridData);
    gridSearch = _postData;
}

function addCode(){
    $('#add').removeClass('hidden');
    $('#sbt').unbind('click').bind('click', function(){
        var _postData2 = {
            Content: {
                Name: $('#m_Name').val(),
                Status: 'O'
            },
            Method: 'A'
        }
        console.log(_postData2)
        for(i in postData){
            _postData2[i] = postData[i]
        }
        if($('#m_Name').val()) {
            $.ajax({
                type: "post",
                url: mdmPath + '/MDM0014/MDM001401',
                data: _postData2,
                dataType: "json",
                success: function(re) {
                    if(re.Status==200){
                        myAlert('保存成功');
                        window.open('MDM001402.html?Code=' + re.Content.Code, '_self');
                    }else{
                        myAlert(re.Message);
                    }
                }
            }); 
        } else {
            myAlert('不能为空')
        }
    })
}